package com.boniu.startapp.services;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.util.Log;


import com.boniu.startapp.Config;
import com.boniu.startapp.MainApplication;
import com.boniu.startapp.SPUtils;

import java.util.Date;

public class NotificationEventReceiver {
    private Context mContext;
    private static NotificationEventReceiver mInstance;
    private boolean nightOpen = true;//下班打卡
    private boolean dayOpen = true;//上班打卡
    public static NotificationEventReceiver register(Context context) {
        if (mInstance == null) {
            mInstance = new NotificationEventReceiver(context);
        }
        return mInstance;

    }

    private NotificationEventReceiver(Context context) {
        mContext = context;
        IntentFilter filter=new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        context.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context1, Intent intent) {
                if (Intent.ACTION_TIME_TICK.equals(intent.getAction())) {
                    if (SPUtils.getInstance().getBoolean(Config.IS_LOCK,true)){
                        Date date = new Date();
                        int hours = date.getHours();
                        int minutes = date.getMinutes();
                        if (hours == 9 && minutes > 31 && dayOpen){
                            startDD(true);
                        }
                        if (hours == 16 && minutes >= 36 && nightOpen){
                            startDD(false);
                        }
                        if (hours > 16 && nightOpen){
                            startDD(false);
                        }
                        Log.e("asd", "onReceive:ACTION_TIME_TICK " + hours + ":" + minutes + ":" + dayOpen + ":" + nightOpen );

                    }
                }
            }
        },filter);
    }

    private void startDD(boolean isDay){
        PackageManager packageManager = mContext.getPackageManager();
        Intent intent=new Intent();
        intent =packageManager.getLaunchIntentForPackage("com.alibaba.android.rimet");
        if (intent != null){
            if (isDay){
                nightOpen = true;
                dayOpen = false;
            }else{
                dayOpen = true;
                nightOpen = false;
            }
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            mContext.startActivity(intent);
        }else{
            Log.e("asd", "onAccessibilityEvent: " );
        }
    }
}
